package com.apes.fn.mdm.paint;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wuq
 * @create 2020-07-30 15:22
 */
@Service
public class PaintBusinessScopeService extends DomainService {

    @Autowired
    private PaintBusinessScopeRepository paintBusinessScopeRepository;

    public PaintBusinessScope create(SimpleRequest request){
        PaintBusinessScope paintScope = request.getO(PaintBusinessScope.class);
        return paintBusinessScopeRepository.saveAndFlush(paintScope);
    }

    public PaintBusinessScope update(SimpleRequest request){
        PaintBusinessScope paintScope = request.getO(PaintBusinessScope.class);
        return paintBusinessScopeRepository.saveAndFlush(paintScope);
    }

    public String delete(SimpleRequest request){
        PaintBusinessScope paintScope = request.getO(PaintBusinessScope.class);
        paintBusinessScopeRepository.delete(paintScope);
        return "删除成功";
    }

    /**
     * 检查油漆采购范围
     * @param request
     */
    public List checkPaintBusinessScope(SimpleRequest request){
        JSONObject jo = request.getJO();
        List<PaintBusinessScope> paintBusinessScopes = paintBusinessScopeRepository.findByDeptIdAndClassifyId(jo.getString("deptId"), jo.getString("classifyId"));
        if (paintBusinessScopes.size()==0)throw new RuntimeException("请联系采购中心配置本基地的油漆采购范围");
        return paintBusinessScopes;
    }

    /**
     * 检查油漆采购价格
     * @param request
     */
    public JSONObject checkPaintBusinessPrice(SimpleRequest request){
        JSONObject jo = request.getJO();
        Map map=MapUtil.mapper("conditionStrategyId", "CG50",
                "SYS_RQ", new Date(),
                "COMPANY_ID", jo.getString("companyId"),
                "PRODUCT_ID", jo.getString("productId"));
        map.put("SUPPLIER_ID", jo.getString("supplierId")!=null?jo.getString("supplierId"):null);
        if (jo.getString("supplierId")==null)map.put("SYS_NO_CHECK", "SUPPLIER_ID");
        Object obj=this.invoke("condition.execConditionStrategy", map);
        JSONObject result=new JSONObject(MapUtil.mapped(obj));
        return result;
    }


}
